//触摸屏获取触摸坐标
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/input.h>

struct input_event ts_buf;
int x,y;
int ts_fd;

int ts_open(void)
{
	//打开触摸屏的驱动
	
	ts_fd = open("/dev/input/event0", O_RDWR);
	if(-1 == ts_fd)
	{
		perror("open ts failed!\n");
		return -1;
	}
}

int ts_get(int *x, int *y)
{
	//读取触摸屏的数据
	bzero(&ts_buf, sizeof(ts_buf));
	while(1)
	{
		read(ts_fd, &ts_buf, sizeof(ts_buf));
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_X)
		{
			*x = ts_buf.value*800/1024;//1024
		}
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_Y)
		{
			*y = ts_buf.value*480/600;//600
		}
		if(ts_buf.type == EV_KEY && ts_buf.code == BTN_TOUCH && ts_buf.value == 0)
		{
			break;
		}
	}
	
}

int main()
{
	ts_open();
	while(1)
	{
		printf("点击获取坐标\n");
		ts_get(&x,&y);
		printf("(%d,%d)\n",x,y);
		//if(x)
	}
	close(ts_fd);
	return 0;
}